import {ElNotification} from "element-plus";
import * as loginApi from "@/api/auth/loginApi.js";
import router from "@/router/index.js";


const getRandomId = () => {
    return Math.floor(Math.random() * 10000000);
}

// valid el form and invoke target method
const validFormAndInvoke = (formEl, success, message = '信息有误', fail = function () {
}) => {
    if (!formEl) {
        return
    }
    formEl.validate(valid => {
        if (valid) {// form valid succeed
            // do success function
            success()
            // reset fields
            formEl.resetFields()
        } else {// form valid fail
            ElNotification({
                title: 'Tips',
                message: message,
                type: 'error',
                duration: 2000
            })
            // do something when fail
            fail()
            return false
        }
    })
}

// 清空对象数据
export const clearObject= (object) => {
    for(let key in object)
        object[key] = '';
}
// 权限是否正确
export const isPermission = (permission) => {
    return localStorage.getItem('role') == permission;
}

export default {
    validFormAndInvoke,
    getRandomId,
}
